<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <script type="text/javascript">
        function check_validationlogin()
        {
            var error = "";
            if(frmLogin.txtUserName.value == "")
                error+= "Username can't not empty!\n";
            if(frmLogin.txtPassword.value == "")
                error+= "Password can't not empty!";
            
            if(error !="")
            {
                alert(error);
                return false;
            }
           
          
        }
    </script>
    <body>
        <?php
        require 'connect.inc';
        require 'session.inc';
        $res = "";
        $state = 0;
        $result;
        $res = " <table width='100%' border='1' cellspacing='0'>
                    <tbody>
                        <tr></tr>
                        <tr>
                            <td>
                                <img src='logo.jpg'>
                            </td>
                            <td>
                                <h1>Photo Album Website</h1>
                            </td>
                        </tr>
                        <tr>
                            <td width='20%' valign='top'>
                                <a href='index.php'>Home</a>
                                <br>
                                <a href='my_albums.php'>My Albums</a>
                                <br>
                                <a href='users.php'>Other persons</a>
                            </td>
                            ";

        if ($_SESSION["isLogin"] == 1)
            $state = 1;
        $username = "";
        $pass = "";
        $check = "";
        if (isset($_GET["btnLogin"])) {

            $username = $_REQUEST["txtUserName"];
            $pass = md5($_REQUEST["txtPassword"]);
            if (isset($_REQUEST["chkRememberMe"]) && $_REQUEST["chkRememberMe"] == 'on') {
                setcookie('username', $username, time() + 60 * 60 * 24 * 365);
                setcookie('password', $_REQUEST["txtPassword"], time() + 60 * 60 * 24 * 365);
                setcookie('remember', "checked", time() + 60 * 60 * 24 * 365);
            } else {
                setcookie('username', "", time() - 3600);
                setcookie('password', "", time() - 3600);
                setcookie('remember', "", time() - 3600);
            }
            $strSQL = "SELECT * FROM users WHERE user_name ='$username' AND password = '$pass'";

            $result = mysql_query($strSQL);
            if ($result && mysql_num_rows($result) > 0) {
                $state = 1;
                $_SESSION["isLogin"] = 1;
                while ($row = mysql_fetch_array($result)) {
                    $_SESSION["iduser"] = $row["user_id"];
                    $_SESSION["username"] = $row["user_name"];
                    $_SESSION["fullName"] = $row["full_name"];
                    $_SESSION["email"] = $row["email"];
                    break;
                }
            }
        }
        if ($state == 1) {
            header("Location: index.php");
        } else {
            if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
                $username = $_COOKIE['username'];
                $pass = $_COOKIE['password'];
                $check = $_COOKIE['remember'];
            }
            $res .= "
                <td>
                    <center>
                        <h2>Please login</h2>
                    </center>
                    <form onsubmit='return check_validationlogin();' action='login.php' method = 'GET' name='frmLogin'>
                    <table>
                        <tbody>
                            <tr>
                                <td>

                                </td>
                            </tr>
                            <tr>
                                <td>User name: </td>
                                <td>
                                    <input type='text' value='$username' name='txtUserName'>
                                </td>
                            </tr>
                            <tr>
                                <td>Password: </td>
                                <td>
                                    <input type='password' value='$pass' name='txtPassword'>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input type='checkbox' checked='$check' name='chkRememberMe'>
                                    Remember me
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input type='submit' value='Login' name='btnLogin'>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </form></td>
                <td width='20%' valign='top'>
                    <a href='login.php'>Login</a>
                    <a href='register.php'>Register</a>
                </td>
        </tr>
    </tbody>
    </table>";
            echo $res;
        }
        ?>
    </body>
</html>
